package com.jzo2o.market.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.redis.sync.SyncManager;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class XxlJobHandler {

    @Resource
    private SyncManager syncManager;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICouponService couponService;

    /**
     * 活动状态修改，
     * 1.活动进行中状态修改
     * 2.活动已失效状态修改
     * 1分钟一次
     */
    @XxlJob("updateActivityStatus")
    public void updateActivitySatus() {
        List<Activity> activityList = activityService.list();
        if (CollectionUtil.isEmpty(activityList)) {
            throw new CommonException("火哦的那个不存在");
        }
        for (Activity activity : activityList) {
            if (LocalDateTime.now().isAfter(activity.getDistributeStartTime()) && LocalDateTime.now().isBefore(activity.getDistributeEndTime()) && activity.getStatus().equals(ActivityStatusEnum.NO_DISTRIBUTE.getStatus())) {
                boolean update = activityService.lambdaUpdate().set(Activity::getStatus, ActivityStatusEnum.DISTRIBUTING.getStatus())
                        .eq(Activity::getId, activity.getId()).update();
                if (!update) {
                    throw new DBException("操作失败");
                }
            }
            if (LocalDateTime.now().isAfter(activity.getDistributeEndTime())) {
                boolean update = activityService.lambdaUpdate().set(Activity::getStatus, ActivityStatusEnum.LOSE_EFFICACY.getStatus())
                        .eq(Activity::getId, activity.getId()).update();
                if (!update) {
                    throw new DBException("操作失败");
                }
            }
        }
    }

    /**
     * 已领取优惠券自动过期任务
     */
    @XxlJob("processExpireCoupon")
    public void processExpireCoupon() {
        List<Coupon> couponList = couponService.list();
        if (CollectionUtil.isEmpty(couponList)){
            throw new CommonException("优惠券不存在");
        }
        for (Coupon coupon : couponList) {
            if (LocalDateTime.now().isAfter(coupon.getValidityTime()) && coupon.getStatus().equals(CouponStatusEnum.NO_USE.getStatus())){
                boolean update = couponService.lambdaUpdate()
                        .eq(Coupon::getId, coupon.getId())
                        .set(Coupon::getStatus, CouponStatusEnum.INVALID.getStatus())
                        .update();
                if (!update){
                    throw new DBException("操作失败");
                }
            }
        }
    }


}
